package com.bl.basic.controller;

import com.bl.basic.exception.BusinessException;
import com.bl.basic.service.IVerifyCodeService;
import com.bl.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;
    /**
     *  获取base64位字符串图形验证码
     * @param key
     * @return
     */
    @RequestMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key){
        try {

           String base64key = verifyCodeService.getVerifyCode(key);


            return JsonResult.me().setResultObj(base64key);

        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("66" + e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙");
        }


    }

    /**
     * 获取手机验证码
     * @param map
     * @return
     */
    @RequestMapping("/smsCode")
    public JsonResult getSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.getSmsCode(map);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙！！！");
        }
    }

    /**
     * 微信扫码登录用户绑定手机号
     * @param map
     * @return
     */
    @RequestMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙！！！");
        }
    }
}
